using System.Collections;
using System.Collections.Generic;
using System.Threading;
using Cysharp.Threading.Tasks;
using UnityEngine;

public class Enemy : Entity
{
    [Header("Move Info")]
    public float currentMoveSpeed;

    [Header("各种时间")]
    protected float idleTime = 1f;
    protected ulong battleTime;
    public float battleDuration = 4f;

    protected Player player;

    [Header("Attack Info")]
    public float attackDistance;
    public float attackCooldown = 1f;

    // Start is called before the first frame update
    protected override void Awake()
    {
        base.Awake();
        currentMoveSpeed = stats.moveSpeed.Value;
    }

    protected override void Start()
    {
        base.Start();
        player = GameMgr.Instance.player;

    }

    protected override void Update()
    {
        base.Update();
    }

    #region  冻结

    /// <summary>
    /// 硬直
    /// </summary>
    /// <param name="isFreeze"></param>
    public virtual void FreezeTime(bool isFreeze)
    {
        isFrozenTime = isFreeze;
        if (isFreeze)
            anim.speed = 0;
        else
            anim.speed = 1;
    }

    public void FreezeTimeFor(float time) => StartCoroutine(FreezeTimeForAsync(time));
    public virtual IEnumerator FreezeTimeForAsync(float time)
    {
        if (time <= 0)
            yield break;
        FreezeTime(true);
        yield return new WaitForSeconds(time);
        FreezeTime(false);
    }
    #endregion

    #region  检测
    protected bool CanAttack()
    {
        if (Time.time >= lastAttackTime + attackCooldown)
        {
            lastAttackTime = Time.time;
            return true;
        }
        return false;
    }

    /// <summary>
    /// 返回值可以是bool也可以是碰到的对象
    /// </summary>
    /// <returns></returns>
    public virtual RaycastHit2D IsPlayerDetected() =>
        Physics2D.Raycast(wallCheck.position, Vector2.right * facingDir, 50f, playerLayer);

    protected override void OnDrawGizmos()
    {
        base.OnDrawGizmos();
        Gizmos.color = Color.yellow;
        Gizmos.DrawLine(
            transform.position,
            transform.position + Vector3.right * facingDir * attackDistance
        );
    }
    #endregion


    #region 动画事件
    public override void AttackTrigger(Collider2D other)
    {
        base.AttackTrigger(other);
    }
    // public override void AttackTrigger()
    // {
    //     base.AttackTrigger();
    //     AttackColliderCheck<Player>((player) => player.BeDamage(this.stats));
    // }
    public virtual void OpenCounterAttackWindow()
    {
        canBeStunnned = true;
        if (CounterImg != null) CounterImg?.SetActive(true);

        AutoCloseCounterAttackWindow();
    }
    // 等待碰撞盒打开，也就是攻击正式执行自动关闭
    protected virtual void AutoCloseCounterAttackWindow()
    {
        StarTaskWithTokenByUntil(() => attackCheckCollider.enabled, () => CloseCounterAttackWindow()).Forget();
    }

    public virtual void CloseCounterAttackWindow()
    {
        canBeStunnned = false;
        if (CounterImg != null) CounterImg?.SetActive(false);
    }
    /// <summary>
    /// 触发眩晕
    /// </summary>
    /// <returns></returns>
    // public virtual bool CanbeStunned()
    // {
    //     if (canBeStunnned)
    //     {
    //         CloseCounterAttackWindow();
    //         return true;
    //     }
    //     return false;
    // }



    #endregion

    #region  状态
    protected IEnumerator Stunned_Enter()
    {
        CloseCounterAttackWindow();
        fx.RedColorBlink();

        rb.velocity = new Vector2(StunDirection.x * facingDir * -1, StunDirection.y);
        // 退出时机只能手动设置，因为公用的同一个动画器
        yield return new WaitForSeconds(StunDuration);
        ChangeState(States.Idle);
    }

    protected void Stunned_Exit()
    {
        fx.CancelColorChange();
    }


    #endregion
}
